package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.DictUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TDemands;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TDemandsService;

/**
 * 用户需求Controller
 * @author wuhao
 * @version 2016-08-08
 */
@Controller
@RequestMapping(value = "${adminPath}/rentproduct/tDemands")
public class TDemandsController extends BaseController {

	@Autowired
	private TDemandsService tDemandsService;
	@Autowired
	private TCargotypeService tCargotypeService;
	
	@ModelAttribute
	public TDemands get(@RequestParam(required=false) String demandid) {
		TDemands entity = null;
		if (StringUtils.isNotBlank(demandid)){
			entity = tDemandsService.get(demandid);
		}
		if (entity == null){
			entity = new TDemands();
		}
		return entity;
	}
	
	@RequiresPermissions("rentproduct:tDemands:view")
	@RequestMapping(value = {"list", ""})
	public String list(TDemands tDemands, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TDemands> page = tDemandsService.findPage(new Page<TDemands>(request, response), tDemands); 
		model.addAttribute("page", page);
		model.addAttribute("tDemands", tDemands);
		return "modules/rentproduct/tDemandsList";
	}

	@RequiresPermissions("rentproduct:tDemands:view")
	@RequestMapping(value = "form")
	public String form(TDemands tDemands, Model model) {
		
		// 如果起止时间都为空，则设置默认起止时间
		if(tDemands.getFromdate() == null) {
			// 租赁开始日期默认第二天及结束日期（默认一年）
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 9);
			calendar.set(Calendar.MINUTE, 0);
			tDemands.setFromdate(calendar.getTime());
		}
		
		// 单选框默认选中设置
		if(StringUtils.isEmpty(tDemands.getIspublic())){
			tDemands.setIspublic(DictUtils.getDictValue("是", "yes_no", ""));
		}
		
		int objCount = 0; // 旅行商数量
		if(!StringUtils.isEmpty(tDemands.getReleaseobject())){
			objCount = tDemands.getReleaseobject().split(",").length;
		}
		// 车型数据
		List<TCargotype> cargotypeList = tCargotypeService.findList(new TCargotype());
		model.addAttribute("cargotypeList", cargotypeList);
		model.addAttribute("tDemands", tDemands);
		model.addAttribute("objCount", objCount);
		return "modules/rentproduct/tDemandsForm";
	}

	@RequiresPermissions("rentproduct:tDemands:edit")
	@RequestMapping(value = "save")
	public String save(TDemands tDemands, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tDemands)){
			return form(tDemands, model);
		}
		tDemands.setReactstatus(TDemands.REACTSTATUS_WAIT);
		tDemandsService.save(tDemands);
		addMessage(redirectAttributes, "保存用户需求成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tDemands/?repage";
	}
	
	@RequiresPermissions("rentproduct:tDemands:edit")
	@RequestMapping(value = "delete")
	public String delete(TDemands tDemands, RedirectAttributes redirectAttributes) {
		tDemandsService.delete(tDemands);
		addMessage(redirectAttributes, "删除用户需求成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tDemands/?repage";
	}

	/**
	 * 需求确定，生成整车发布
	 */
	@RequiresPermissions("rentproduct:tDemands:edit")
	@RequestMapping(value = "sure")
	public String sure(TDemands tDemands, RedirectAttributes redirectAttributes) {
		try {
			if(tDemandsService.sure(tDemands)){
				addMessage(redirectAttributes, "需求发布成功");
			} else {
				addMessage(redirectAttributes, "需求发布失败");
			}
		} catch (Exception e) {
			addMessage(redirectAttributes, "数据库繁忙，请稍后再操作");
		}
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tDemands/?repage";
	}
}